<layout name="no-slide"/>

<section class="haggle-intro">
  <h3 class="haggle-intro-title">非常“辉”砍价</h3>
  <p>购车找晓辉！</p>
  <p>晓辉在路上为粉丝争取更多购车优惠，在您买车时，把您看中的车型告诉《晓辉在路上》，非常“辉”砍价会在您拿到的优惠低价上，再次出击，更加优惠！</p>
</section>

<header class="haggle-signup">
  <a id="signup" class="btn btn-blue" href="javascript:;">我要报名</a>
</header>

<section class="haggle-4sshop">
  <h3 class="forshop-title">合作商家</h3>

  <volist name="haggleShopList" id="vo" key="index">
    <if condition="($index-1)%2 eq 0"><div class="fun-grid"></if>
      <div class="forshop-list col-xs-6">
        <a href="{$vo.url}">
          <span class="forshop-logo"><img src="{$vo.logo}" alt=""></span>
          <div class="forshop-info">
            <p class="ellipsis">{$vo.name}</p>
          </div>
        </a>
      </div>
    <if condition="$index%2 eq 0"></div></if>
  </volist>

  </div>
</section>

<!-- 报名表单 -->
<section id="popup" class="popup">
  <div class="navbar">
    <h3 class="title">报名非常“辉”砍价<span class="close">关闭</span></h3>
  </div>
  <div id="popup-content" class="content">
    <form id="form" class="form form-simple" method="post" action="__CONTROLLER__/save">
    
      <div class="form-item fun-grid">
        <label class="col-md-3">您的姓名</label>
        <p class="col-md-9">
          <input type="text" name="name">
        </p>
      </div>
      
      <div class="form-item fun-grid">
        <label class="col-md-3">电话</label>
        <p class="col-md-9">
          <input type="tel" name="tel">
        </p>
      </div>

      <div class="form-item fun-grid">
        <label class="col-md-3">所选车型</label>
        <p class="col-md-9">
          <input type="text" name="type">
        </p>
      </div>

      <div class="form-item fun-grid">
        <label class="col-md-3">选择的4S店</label>
        <p class="col-md-9">
          <input type="text" name="shop_name">
        </p>
      </div>

      <div class="form-item fun-grid">
        <label class="col-md-3">您已砍到的价格</label>
        <p class="col-md-9">
          <input type="text" name="price">
        </p>
      </div>
      
      <div class="form-item fun-grid">
        <input type="hidden" name="sid" value="{$_GET['sid']}">
        <input type="hidden" name="open_id" value="{$open_id}">
        <p class="submit col-md-offset-3">
          <button class="btn btn-blue" type="submit">报名参与</button>
        </p>
      </div>
      
    </form>
  </div>
  
</section>

<script type="text/javascript">

  // 加载modal.js
  GH.load( "js", "js-modal", "__COMMON__/js/modal.js" );

  var pop = document.getElementById( "popup" ),
      signup = document.getElementById( "signup" ),
      inputs = document.querySelectorAll( "#form input" );

  signup.addEventListener("touchend", function(e){
    e.preventDefault();
    pop.classList.add( "popup-in" );
  }, false);

  document.querySelector("#popup .close").addEventListener("touchend", function(){
    pop.classList.remove( "popup-in" );
  }, false);

  GH.$("#form").addEventListener("submit", check, false);

  function check( e ) {
    e.preventDefault();
    var canpost = true,
        i = 0,
        item;
    for(; item = inputs[i]; i++) {
      if ( item.value.trim() === "" && item.getAttribute("type") !== "hidden" ) {
        GH.showMessage("请填写完整信息");
        canpost = false;
        return;
      };
    };
    canpost && signupForm();
  };

  function signupForm() {
    GH.ajax("__CONTROLLER__/save", GH.serialize("form"), function(data){
      if ( data.error === 0 ) {
        pop.classList.remove( "popup-in" );
        modal.alert({
          title: "报名结果",
          text: "晓辉已经收到您的砍价信息，耐心等待好消息吧",
          buttons: [{ text: "好的", close: true, click: goIndex }]
        })
      } else {
        GH.showMessage(data.msg);
      }
    });
  };

  function goIndex() {
    window.location = "{:U('Site/index', 'sid='.$_GET['sid'])}";
  };

</script>